﻿using Microsoft.AspNetCore.Mvc;

namespace Netnr.DataKit.Web.Controllers
{
    /// <summary>
    /// Netnr.DataKit API
    /// </summary>
    [Route("[controller]/[action]")]
    [ApiController]
    [ResponseCache(Duration = 3)]
    [Filters.FilterConfigs.AllowCors]
    public class DKController : ControllerBase
    {
        /// <summary>
        /// 获取所有表名及注释
        /// </summary>
        /// <param name="tdb">数据库类型（0：MySQL，1：SQLite，2：Oracle，3：SQLServer，4：PostgreSQL）</param>
        /// <param name="conn">连接字符串</param>
        /// <returns></returns>
        [HttpGet]
        public ActionResultVM GetTable(DB.TypeDB? tdb, string conn)
        {
            var vm = new Func.DK().GetTable(tdb, conn);
            return vm;
        }

        /// <summary>
        /// 获取所有列
        /// </summary>
        /// <param name="tdb">数据库类型（0：MySQL，1：SQLite，2：Oracle，3：SQLServer，4：PostgreSQL）</param>
        /// <param name="conn">连接字符串</param>
        /// <param name="filterTableName">过滤表名，英文逗号分隔，为空时默认所有表</param>
        /// <returns></returns>
        [HttpPost]
        public ActionResultVM GetColumn([FromForm]DB.TypeDB? tdb, [FromForm]string conn, [FromForm]string filterTableName = "")
        {
            var vm = new Func.DK().GetColumn(tdb, conn, filterTableName);
            return vm;
        }

        /// <summary>
        /// 设置表注释
        /// </summary>
        /// <param name="tdb">数据库类型（0：MySQL，1：SQLite，2：Oracle，3：SQLServer，4：PostgreSQL）</param>
        /// <param name="conn">连接字符串</param>
        /// <param name="TableName">表名</param>
        /// <param name="TableComment">表注释</param>
        /// <returns></returns>
        [HttpGet]
        public ActionResultVM SetTableComment(DB.TypeDB? tdb, string conn, string TableName, string TableComment)
        {
            var vm = new Func.DK().SetTableComment(tdb, conn, TableName, TableComment);
            return vm;
        }

        /// <summary>
        /// 设置列注释
        /// </summary>
        /// <param name="tdb">数据库类型（0：MySQL，1：SQLite，2：Oracle，3：SQLServer，4：PostgreSQL）</param>
        /// <param name="conn">连接字符串</param>
        /// <param name="TableName">表名</param>
        /// <param name="FieldName">列名</param>
        /// <param name="FieldComment">列注释</param>
        /// <returns></returns>
        [HttpGet]
        public ActionResultVM SetColumnComment(DB.TypeDB? tdb, string conn, string TableName, string FieldName, string FieldComment)
        {
            var vm = new Func.DK().SetColumnComment(tdb, conn, TableName, FieldName, FieldComment);
            return vm;
        }
    }
}